package com.zzq.controller;

import com.zzq.pojo.Message;
import com.zzq.pojo.Result;
import com.zzq.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/list")
    public Result<List<Message>> getMessageList(){
        List<Message> messageList = messageService.getMessageList();
        return Result.success(messageList);
    }

    @PutMapping("/delete")
    public Result deleteMessage(int id){
        messageService.deleteMessage(id);
        return Result.success();
    }

    @PostMapping("/add")
    public Result addMessage(@RequestBody Message message){
        messageService.addMessage(message);
        log.info("type:{}",message.getType());
        return Result.success();
    }

    @GetMapping("/newMessage")
    public Result<List<Message>> newMessage( @RequestParam(defaultValue = "0") int page,
                                             @RequestParam(defaultValue = "4") int size){


        List<Message> newMessage = messageService.getNewMessageList(page, size);
        return Result.success(newMessage);
    }

    @GetMapping("/getNoticeById")
    public Result<Message> getNoticeById(int id){
        Message message=messageService.getNoticeById(id);
        return Result.success(message);
    }
}
